package com.car.loan.framework.aspectj.impl;

import com.car.loan.common.enums.LoanStatusEnum;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.DataStatusSqlService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.BusinessApostilleStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessAuditStatus;
import org.springframework.stereotype.Service;

/**
 * @author huangding
 * @description
 * @date 2019/11/26 16:22
 */
@Service
public class BusinessApostilleStatusSqlServiceImpl implements DataStatusSqlService {

    @Override
    public LoanStatusEnum getLoanStatusEnum() {
        return LoanStatusEnum.BUSINESS_APOSTILLE_STATUS;
    }

    @Override
    public String getSql(String statusAlias, Integer status) {
        Long loginUserId = SecurityUtils.getLoginUserId();
        String sql =
            " loan.second_audit_id = " + loginUserId + "  and  {}.file_audit_status = "
                + FileAuditStatus.INIT.status + " and {}.business_audit_status = "
                + LoanBusinessAuditStatus.APOSTILLE.status;
        if (status != null) {
            switch (BusinessApostilleStatus.get(status)
                .orElse(BusinessApostilleStatus.INIT)) {
                case INIT:
                    sql +=
                        " and {}.business_apostille_status =  "
                            + BusinessApostilleStatus.INIT.status;
                    break;
                case PASS:
                    sql +=
                        " and  {}.business_apostille_status= "
                            + BusinessApostilleStatus.PASS.status;
                    break;
                case GO_BACK:
                    sql += " and {}.business_apostille_status = "
                        + BusinessApostilleStatus.GO_BACK.status;
                    break;
                case REJECT:
                    sql += " and  {}.business_apostille_status = "
                        + BusinessApostilleStatus.REJECT.status;
                    break;
                default:

            }
        }
        return sql.replace("{}", statusAlias);
    }

    @Override
    public String getSortSql(String statusAlias) {
        return " order by {}.business_audit_time desc".replace("{}", statusAlias);
    }

    @Override
    public LoanBaseInfo getStatusRemark(LoanBaseInfo loanBaseInfo) {
        Long businessApostilleStatus = loanBaseInfo.getBusinessApostilleStatus();
        String statusRemark = StringUtils.EMPTY;
        if (BusinessApostilleStatus.INIT.status == businessApostilleStatus) {
            statusRemark = "待审批";
        } else if (businessApostilleStatus == BusinessApostilleStatus.PASS.status) {
            statusRemark = "已审批";
        } else if (businessApostilleStatus == BusinessApostilleStatus.GO_BACK.status) {
            statusRemark = "已退回";
        } else if (businessApostilleStatus == BusinessApostilleStatus.REJECT.status) {
            statusRemark = "已拒绝";
        }
        loanBaseInfo.setStatusRemark(statusRemark);
        return loanBaseInfo;
    }


}
